#include "Logic_PowerStapler.h"

//*********************************************************************
/* ElasticClosure Motor Driver
 * input 
 * return: 
 * author: Guichuan.He
 * date: 2016-10-09
 */
//**********************************************************************

uint Logic_MotorDriver_ElasticClosure()
{
  

    Device_MotorSetCurrent(PowerStaplerData.MotorControl.ElasticClosureCurrent);
     Device_MotorOutControl(FORWARD);

    while (1) //close
    {
        if (Logic_MotorDriver_CheckCloseEnd() == END) {
            Device_MotorOutControl(STOP);
            return 0; //close end
        }
        if (Device_Trigger_CheckFire() == SWITCH_OFF) {
            Device_MotorOutControl(STOP);
            return 1; //Trigger A is Open
        }
        // if over time 
        // return 2;
    }
}

//*********************************************************************
/* ElasticOpen Motor Driver
 * input 
 * return: 
 * author: Guichuan.He
 * date: 2016-10-09
 */
//**********************************************************************

uint Logic_MotorDriver_ElasticOpen() {
    Device_MotorSetCurrent(PowerStaplerData.MotorControl.ElasticOpenCurrent);
    PowerStaplerData.MotorControl.Director = 0; //BACK
    Device_MotorOutControl(BACK);
    while (1) {
        if (Logic_MotorDriver_CheckOpenEnd() == END) {
            Device_MotorOutControl(STOP);
            return 0; // Open is OK  
        }

        if (Device_Trigger_CheckFire() == SWITCH_ON) {
            Device_MotorOutControl(STOP);
            return 1;
        }
        // if over time 
        // return 3;
    }
}

//*********************************************************************
/* Cut ready Motor Driver
 * input 
 * return: 
 * author: Guichuan.He
 * date: 2016-10-26
 */
//**********************************************************************

uint Logic_MotorDriver_Phase_Safty() 
{
    uchar Trigger_C_Flag = 0;

        // return 2;
  

}

//*********************************************************************
/* from safty to Standby
 * input 
 * return: 
 * author: Guichuan.He
 * date: 2016-10-28
 */
//**********************************************************************

uint Logic_MotorDriver_SaftyToOpenValue() {
    Device_MotorSetCurrent(PowerStaplerData.MotorControl.ElasticOpenCurrent);
    PowerStaplerData.MotorControl.Director = 0; //BACK
    Device_MotorOutControl(BACK);
    while (1) {
        if (Logic_MotorDriver_CheckOpenEnd() == END) {
            Device_MotorOutControl(STOP);
            while (Device_Trigger_CheckFire() == SWITCH_ON);
            return 0; // Open is OK  
        }
        // if over time 
        // return 3;
    }
}
//*********************************************************************
/* Trigger c befor cut
 * input 
 * return: 
 * author: Guichuan.He
 * date: 2016-10-09
 */
//**********************************************************************

uint Logic_MotorDriver_CutReady() {
    uchar Trigger_C_Flag = 0;

 
}
//*********************************************************************
/* cutting Motor Driver
 * input 
 * return: 
 * author: Guichuan.He
 * date: 2016-10-09
 */
//**********************************************************************

uint Logic_MotorDriver_Cutting() {
    PowerStaplerData.MotorControl.Turns_Number = CutTurns;
    Device_MotorSetCurrent(PowerStaplerData.MotorControl.CuttingCurrent);
    PowerStaplerData.MotorControl.Director = 1; //forward
    Device_MotorOutControl(FORWARD);

    while (1) //break 1-fire trigger off 2 - cut end
    {
        if (Logic_MotorDriver_CheckCuttingEnd() == END)
            return 0;
        if (Device_Trigger_CheckFire() == SWITCH_OFF) {
            Device_MotorOutControl(STOP);
            return 1;
        }
        // if over time 
        // return 2;
    }

}

//*********************************************************************
/*
 * input 
 * return: 
 * author: Guichuan.He
 * date: 2016-10-28
 */
//**********************************************************************

uint Logic_MotorDriver_CutPause() {
    uchar Trigger_C_Flag = 0;

    while (1) {
        if (Trigger_C_Flag == 0) {
            PowerStaplerData.Indicator.ForwardLed_Flash = 1;
            PowerStaplerData.Indicator.BackLED_Flash = 0;
        } else {
            PowerStaplerData.Indicator.ForwardLed_Flash = 0;
            PowerStaplerData.Indicator.BackLED_Flash = 1;
        }

        if (Device_Trigger_CheckFire() == SWITCH_ON) {
            //  while(Device_Trigger_CheckFire() == SWITCH_ON); 
            if (Trigger_C_Flag == 0)
                return 0; //Trigger B is close  cutting
            else
                return 1; //saft_to_open
        }

        if (Device_Trigger_CheckDir() == SWITCH_ON) {
            if (Trigger_C_Flag == 0) {
                PowerStaplerData.Indicator.ForwardLed_Flash = 0;
                PowerStaplerData.Indicator.BackLED_Flash = 1;
                Trigger_C_Flag = 1;
            } else {
                PowerStaplerData.Indicator.ForwardLed_Flash = 1;
                PowerStaplerData.Indicator.BackLED_Flash = 0;
                Trigger_C_Flag = 0;
            }
            while (Device_Trigger_CheckDir() == SWITCH_ON);
        }
        // if over time 
        // return 2;
    }


}

//*********************************************************************
/* 
 * input 
 * return: 
 * author: Guichuan.He
 * date: 2016-10-28
 */
//**********************************************************************

uint Logic_MotorDriver_CutEnd() {
    uchar Trigger_C_Flag = 0;

    while (Device_Trigger_CheckFire() == SWITCH_ON);
    while (1) {
        if (Trigger_C_Flag == 0) {
            PowerStaplerData.Indicator.ForwardLed_Flash = 0;
            PowerStaplerData.Indicator.BackLED_Flash = 0;
            Device_Indicator_SetDirectorLED(3, 0);
        } else {
            PowerStaplerData.Indicator.ForwardLed_Flash = 0;
            PowerStaplerData.Indicator.BackLED_Flash = 1;
        }

        if (Device_Trigger_CheckFire() == SWITCH_ON) {
            if (Trigger_C_Flag == 1) //trigger c is close
                return 0; //Trigger c is close  
            else
                return 1; //saft_to_open
        }
        if (Device_Trigger_CheckDir() == SWITCH_ON) {
            if (Trigger_C_Flag == 0) {
                PowerStaplerData.Indicator.ForwardLed_Flash = 0;
                PowerStaplerData.Indicator.BackLED_Flash = 1;
                Trigger_C_Flag = 1;
            } else {
                PowerStaplerData.Indicator.ForwardLed_Flash = 0;
                PowerStaplerData.Indicator.BackLED_Flash = 0;
                Device_Indicator_SetDirectorLED(3, 0);
                Trigger_C_Flag = 0;
            }
            while (Device_Trigger_CheckDir() == SWITCH_ON);
        }
        // if over time 
        // return 2;
    }

}
//*********************************************************************
/* return Motor Driver
 * input 
 * return: 
 * author: Guichuan.He
 * date: 2016-10-09
 */
//**********************************************************************

uint Logic_MotorDriver_Return() {
    Device_MotorSetCurrent(PowerStaplerData.MotorControl.ReturnCurrent);
    PowerStaplerData.MotorControl.Director = 0; //BACK
    Device_MotorOutControl(BACK);
    while (1) //break 1-fire trigger off 2 - cut end
    {
        if (Device_Trigger_CheckFire() == SWITCH_OFF) {
            Device_MotorOutControl(STOP);
            return 1;
        }

        if (Logic_MotorDriver_CheckReturnEnd() == END) {
            while (Device_Trigger_CheckFire() == SWITCH_ON);
            return 0;
        }
    }

}

//*********************************************************************
/* check cut end
 * input 
 * return: 
 * author: Guichuan.He
 * date: 2016-10-10
 */
//**********************************************************************

uint Logic_MotorDriver_ReturnPause()
{
    uchar Trigger_C_Flag = 0;

    while (1) 
    {
        if (Trigger_C_Flag == 0) 
        {
            PowerStaplerData.Indicator.ForwardLed_Flash = 0;
            PowerStaplerData.Indicator.BackLED_Flash = 1;
        }
        else 
        {
            PowerStaplerData.Indicator.ForwardLed_Flash = 1;
            PowerStaplerData.Indicator.BackLED_Flash = 0;
        }

        if (Device_Trigger_CheckFire() == SWITCH_ON) 
        {
            if (Trigger_C_Flag == 0) //trigger c is close
                return 0; 
            else
                return 1; 
        }
        if (Device_Trigger_CheckDir() == SWITCH_ON) 
        {
            if (Trigger_C_Flag == 0) 
            {
                PowerStaplerData.Indicator.ForwardLed_Flash = 1;
                PowerStaplerData.Indicator.BackLED_Flash = 0;
                Trigger_C_Flag = 1;
            }
            else
            {
                PowerStaplerData.Indicator.ForwardLed_Flash = 0;
                PowerStaplerData.Indicator.BackLED_Flash = 1;
                Trigger_C_Flag = 0;
            }
            while (Device_Trigger_CheckDir() == SWITCH_ON);
        }
        // if over time 
        // return 2;
    }

}

//*********************************************************************
/* check cut end
 * input 
 * return: 
 * author: Guichuan.He
 * date: 2016-10-10
 */
//**********************************************************************

uint Logic_MotorDriver_CheckCuttingEnd() {
    if (PowerStaplerData.MotorControl.FeedbackStatus == 1) {
        PowerStaplerData.MotorControl.FeedbackStatus = 0;
        return 0;
    } else {
        return 1;
    }
}

//*********************************************************************
/* check open end
 * input 
 * return: 
 * author: Guichuan.He
 * date: 2016-10-10
 */
//**********************************************************************

uint Logic_MotorDriver_CheckOpenEnd() {
    if (PowerStaplerData.MotorControl.FeedbackStatus == 1) {
        PowerStaplerData.MotorControl.FeedbackStatus = 0;
        return 0;
    } else {
        return 1;
    }
}

//*********************************************************************
/* check close end
 * input 
 * return:  0 - stop 1- CONTINUE
 * author: Guichuan.He
 * date: 2016-10-10
 */
//**********************************************************************

uint Logic_MotorDriver_CheckCloseEnd() {
    if (PowerStaplerData.MotorControl.FeedbackStatus == 1) {
        PowerStaplerData.MotorControl.FeedbackStatus = 0;
        return 0;
    } else {
        return 1;
    }
}

//*********************************************************************
/* check return end
 * input 
 * return: 
 * author: Guichuan.He
 * date: 2016-10-10
 */
//**********************************************************************

uint Logic_MotorDriver_CheckReturnEnd() {
    if (PowerStaplerData.MotorControl.FeedbackStatus == 1) {
        PowerStaplerData.MotorControl.FeedbackStatus = 0;
        return 0;
    } else {
        return 1;
    }

}